///////////////////////////////////////////////////////////////////
// Student name:Jon Hersack and Caleb Reinking
// Course: COSC 3403
// Assignment: #7 - Initial Software Construction
// File name:Team.java
// Purpose: Tracks team score and manages active players and
//          distributes cards to players
// Assumptions: constructor assumes correct input of team type
// Limitations:none known
// Development Computer:Intel core2 duo 2.67Ghz
// Operating System: windows
// Integrated Development Environment (IDE): netbeans 6.5
// Compiler: Javac: java 1.6.0_13
///////////////////////////////////////////////////////////////////

import java.util.*;

public class Team
{
    Vector<Player> playerVect;
    int gameScore;
    int handMileage;
    int activePlayer;
    int twoHundredsLeft;
    boolean speedLimit;
    Vector<Pile> pileVect;//pile 0 = speed, 1 = battle
    Boolean[] safetyArea = new Boolean[4];
    //order:ROW, DA, PP, ET
    Boolean[] hazardStatus = new Boolean[5];
    //order: stop, speedLimit, out of gas, flattire, accident
    String name;

    //##########################################
    Team(int teamType, String teamName)
    {

        playerVect = new Vector<Player>(2);//two players per team

        if(teamType==1)//add a human player
        {
            playerVect.add(new HumanPlayer(true));
        }//end if
        else//add a computer player
        {
            playerVect.add(new ComputerPlayer(false));
        }//end else
        
        playerVect.add(new ComputerPlayer(false));//add second player
        
        gameScore = 0;
        handMileage = 0;
        activePlayer = 0;
        twoHundredsLeft = 2;
        pileVect = new Vector<Pile>(2);
        pileVect.add(new Pile());
        pileVect.add(new Pile());

        for( int i = 0 ; i < safetyArea.length ; i++ )
        {
            safetyArea[i] = false;
        }

        resetHazards();

        name = teamName;
    }//end constructor

    //############################################
    Vector<Player> getPlayers()
    {
        return playerVect;
    }// end getPlayers

    //############################################
    void playOnPile(Card card)
    {
        switch( card.getTypeID() )
        {
            case 1: addMiles(25);
                    break;
            case 2: addMiles(50);
                    break;
            case 3: addMiles(75);
                    break;
            case 4: addMiles(100);
                    break;
            case 5: addMiles(200);
                    break;
                //card goes to distance pile
               // pileVect.elementAt(0).playCard(card);
                //add to score
            case 6:
                  pileVect.elementAt(1).playCard(card);
                  resetHazards();
                  hazardStatus[2]=true;
                  break;
            case 7:
                  pileVect.elementAt(1).playCard(card);
                  resetHazards();
                  hazardStatus[3]=true;
                  break;
            case 8:
                  pileVect.elementAt(1).playCard(card);
                  resetHazards();
                  hazardStatus[3]=true;
                  break;
            case 10: // card goes on other team's battle pile
                  pileVect.elementAt(1).playCard(card);
                  resetHazards();
                  break;
            case 9: // card goes on other team's speed pile
                resetHazards();
                hazardStatus[0]=false;//don't erase roll on speed lmit play
                hazardStatus[1]=true;
                pileVect.elementAt(0).playCard(card);
                     break;
            case 11://gas
                //hazard order: stop, speedLimit, out of gas, flattire, accident
                hazardStatus[2]=false;
                pileVect.elementAt(1).playCard(card);
                    break;
            case 12://tire
                hazardStatus[3]=false;
                pileVect.elementAt(1).playCard(card);
                    break;
            case 13://repair
                hazardStatus[4]=false;
                pileVect.elementAt(1).playCard(card);
                    break;
            case 15://roll
                hazardStatus[0]=false;
                pileVect.elementAt(1).playCard(card);
                    break;
            case 14://limit
                pileVect.elementAt(0).playCard(card);
                hazardStatus[1] = false;
                    break;
            // card goes on own team's safety pile
            case 16: safetyArea[3]=true;//et
                    break;
            case 17: safetyArea[2]=true;//pp
                    break;
            case 18: safetyArea[1]=true;//da
                    break;
            case 19: safetyArea[0]=true;//row
                    break;
        }//end switch
    }// end getPlayers

    //############################################
    Player getPlayer(int index)
    {
        return playerVect.get(index);
    } // end getPlayer

    //############################################
    void addMiles(int miles)
    {
        //adds miles if less than equal to 1000
        if(handMileage+miles<=1000)
        {
            handMileage+=miles;
            gameScore+=miles;
        }//end if
        //ignores and discard if greater than trip of 1000
    } // end getPlayer

    //############################################
    Player getActivePlayer()
    {
        return playerVect.get(activePlayer);
    } // end getPlayer

    //############################################
    void toggleActivePlayer()
    {

    }//end toggleACtivePlayer

    //############################################
    void startTurn()
    {

        
    }//end startTurn

    //############################################
    Card feedMe()
    {//might not need
        Card newCard = new Card();
        return newCard;
    }//end feedme

    //############################################
    void giveCard(Card newCard)
    {
        getActivePlayer().addCard(newCard);
    }//end giveCard

    //############################################
    String getSafety(int index)
    {
        if(safetyArea[index])
        return "YES";
        else
        return "NO";
    }//end giveCard

        //############################################
    String getSpeedLimit()
    {
        if(hazardStatus[1])
        return "25/50";
        else
        return "NONE";
    }//end giveCard

    //############################################
    boolean checkValidDistance(int miles)
    { //order: stop, speedLimit, out of gas, flat-tire, accident
        if(hazardStatus[0]==false||safetyArea[0]==true)
        {//we have roll or right of way, check for other hazards

            if(hazardStatus[1]==true)
            {//speed limite active
                if(miles>50)
                    return false;
            }//end if limit

             if(hazardStatus[2]==true||hazardStatus[3]==true||hazardStatus[4]==true)
            {//hazards are active
                return false;
            }//end if

             else//no hazard+roll
             {
                if(miles!=200)
                {
                    return true;
                }
                else//we have a 200
                {
                    if(twoHundredsLeft>0)
                    {//we have not played too many two hundreds
                    twoHundredsLeft--;
                    return true;
                     }
                    else
                        return false;
                }//end else
             }//end else
        }//end if
        else//no roll or row card
        {return false;}//end else
    }//end giveCard

    //#########################################################33
    void resetHazards()
    {
        for( int i = 0 ; i < hazardStatus.length ; i++ )
        {
            hazardStatus[i] = false;
        }
            hazardStatus[0]=true;//set stop to true so we need a roll to play
    }//end rsetHazards

    void resetSafety()
    {
            for( int i = 0 ; i < safetyArea.length ; i++ )
        {
            safetyArea[i] = false;
        }
    }//end resetSafety

    void resetTeam()
    {
    resetSafety();
    resetHazards();
    twoHundredsLeft = 2;
    }
//#########################################################33
     boolean checkValidHazard(int ID)
    { //order: stop, speedLimit, out of gas, flat-tire, accident
        boolean valid = false;
         switch(ID)
            {
             case 6://out of gas
                if(safetyArea[3]==false)//et
                {
                  valid = true;
                }//end if
                    break;
            case 7://flat tire
                if(safetyArea[2]==false)//pp
                {
                  valid = true;
                }//end if
                    break;
            case 8://accident
                if(safetyArea[1]==false)//da
                {
                  valid = true;
                }//end if
                    break;
            case 9://speedlmit
                if(safetyArea[0]==false)//row
                {
                  valid = true;
                }//end if
                    break;
            case 10://stop
                if(safetyArea[0]==false)//row
                {
                  valid = true;
                }//end if
                    break;
        }//end switch
        return valid;

    }//end validhazard

//#########################################################33
     boolean checkValidRemedy(int ID)
    { //hazard order: stop, speedLimit, out of gas, flat-tire, accident
        boolean valid = false;
         switch(ID)
        {
            case 11:
                if(hazardStatus[2]==true)//gas
                {
                  valid = true;
                }//end if
                 break;
            case 12:
                if(hazardStatus[3]==true)//tire
                {
                  valid = true;
                }//end if
                  break;
             case 13:
                 if(hazardStatus[4]==true)//accident
                {
                  valid = true;
                }//end if
                  break;
             case 15:
                 if(hazardStatus[0]==true&&safetyArea[0]==false)//limit
                {
                  valid = true;
                }//end if
                    break;
            case 14: 
                if(hazardStatus[1]==true)//limit
                {
                  valid = true;
                }//end if
                    break;
        }//end switch
        return valid;

    }//end validremedy

    //############################################
    void giveHands(Vector<Card> teamHands)
    {//takes 12 card vector to distribute to players' hands
        playerVect.get(0).getHand().clearHand();
        playerVect.get(1).getHand().clearHand();
        for( int i = 0 ; i < teamHands.size()/2 ; i++ )
        {
            playerVect.elementAt(0).addCard(teamHands.elementAt(i));
        }
        for( int j = teamHands.size()/2 ; j < teamHands.size() ; j++ )
        {
            playerVect.elementAt(1).addCard(teamHands.elementAt(j));
        }

    }//end giveCard

    //############################################
    @Override
    public String toString()
    {
        return "Team Name: " + name + "\n" +
               "------------------------------" + "\n" +
               playerVect.elementAt(0).toString() + "\n" +
               playerVect.elementAt(1).toString();
    }

}//end class Team
